//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.jim.server.helper.redis;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jim.core.ImChannelContext;
import org.jim.core.ImSessionContext;
import org.jim.core.cache.redis.RedisCache;
import org.jim.core.cache.redis.RedisCacheManager;
import org.jim.core.config.ImConfig;
import org.jim.core.config.ImConfig.Global;
import org.jim.core.exception.ImException;
import org.jim.core.listener.AbstractImStoreBindListener;
import org.jim.core.message.MessageHelper;
import org.jim.core.packets.Group;
import org.jim.core.packets.User;
import org.jim.core.packets.UserStatusType;
import org.jim.server.config.ImServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RedisImStoreBindListener extends AbstractImStoreBindListener {
	private static Logger logger = LoggerFactory.getLogger(RedisImStoreBindListener.class);
	private static final String SUFFIX = ":";

	public RedisImStoreBindListener(ImConfig imConfig, MessageHelper messageHelper) {
		super(imConfig, messageHelper);
	}

	public void onAfterGroupBind(ImChannelContext imChannelContext, Group group) throws ImException {
		if (this.isStore()) {
			this.initGroupUsers(group, imChannelContext);
		}
	}

	public void onAfterGroupUnbind(ImChannelContext imChannelContext, Group group) throws ImException {
		if (this.isStore()) {
			String userId = imChannelContext.getUserId();
			String groupId = group.getGroupId();
			RedisCacheManager.getCache("group").listRemove(groupId + ":" + "user", userId);
			RedisCacheManager.getCache("user").listRemove(userId + ":" + "group", groupId);
			RedisCacheManager.getCache("push").remove("group:" + group + ":" + userId);
		}
	}

	public void onAfterUserBind(ImChannelContext imChannelContext, User user) throws ImException {
		if (this.isStore() && !Objects.isNull(user)) {
			user.setStatus(UserStatusType.ONLINE.getStatus());
			this.messageHelper.updateUserTerminal(user);
			this.initUserInfo(user);
		}
	}

	public void onAfterUserUnbind(ImChannelContext imChannelContext, User user) throws ImException {
		if (this.isStore() && !Objects.isNull(user)) {
			user.setStatus(UserStatusType.OFFLINE.getStatus());
			this.messageHelper.updateUserTerminal(user);
		}
	}

	public void initGroupUsers(Group group, ImChannelContext imChannelContext) {
		String groupId = group.getGroupId();
		if (this.isStore()) {
			String userId = imChannelContext.getUserId();
			if (!StringUtils.isEmpty(groupId) && !StringUtils.isEmpty(userId)) {
				String group_user_key = groupId + ":" + "user";
				RedisCache groupCache = RedisCacheManager.getCache("group");
				List<String> users = groupCache.listGetAll(group_user_key);
				if (!users.contains(userId)) {
					groupCache.listPushTail(group_user_key, userId);
				}

				this.initUserGroups(userId, groupId);
				ImSessionContext imSessionContext = imChannelContext.getSessionContext();
				User onlineUser = imSessionContext.getImClientNode().getUser();
				if (onlineUser != null) {
					List<Group> groups = onlineUser.getGroups();
					if (groups != null) {
						Iterator var11 = groups.iterator();

						while(var11.hasNext()) {
							Group storeGroup = (Group)var11.next();
							if (groupId.equals(storeGroup.getGroupId())) {
								groupCache.put(groupId + ":" + "info", storeGroup);
								break;
							}
						}

					}
				}
			}
		}
	}

	public void initUserGroups(String userId, String group) {
		if (this.isStore()) {
			if (!StringUtils.isEmpty(group) && !StringUtils.isEmpty(userId)) {
				List<String> groups = RedisCacheManager.getCache("user").listGetAll(userId + ":" + "group");
				if (!groups.contains(group)) {
					RedisCacheManager.getCache("user").listPushTail(userId + ":" + "group", group);
				}
			}
		}
	}

	public void initUserInfo(User user) {
		if (this.isStore() && user != null) {
			String userId = user.getUserId();
			if (!StringUtils.isEmpty(userId)) {
				RedisCache userCache = RedisCacheManager.getCache("user");
				userCache.put(userId + ":" + "info", user.clone());
				List<Group> friends = user.getFriends();
				if (!CollectionUtils.isEmpty(friends)) {
					userCache.put(userId + ":" + "friends", (Serializable)friends);
				}
			}
		}
	}

	public boolean isStore() {
		ImServerConfig imServerConfig = (ImServerConfig)Global.get();
		return ImServerConfig.ON.equals(imServerConfig.getIsStore());
	}

	static {
		RedisCacheManager.register("user", Integer.MAX_VALUE, Integer.MAX_VALUE);
		RedisCacheManager.register("group", Integer.MAX_VALUE, Integer.MAX_VALUE);
		RedisCacheManager.register("store", Integer.MAX_VALUE, Integer.MAX_VALUE);
		RedisCacheManager.register("push", Integer.MAX_VALUE, Integer.MAX_VALUE);
	}
}
